﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeFrame.Core;
using WeFrame.Core.Common;
using WeFrame.Core.Model;
using WeFrame.Core.BLL;

namespace WeFrame.Web.Admin.SystemSetting
{
    public partial class SiteConfig :  WeFrame.Core.UI.AdminPage
    {
        string defaultPassword = "password"; //默认显示密码
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("SysConfig", WeFrameEnums.ActionEnum.View.ToString()); //检查权限
                ShowInfo();
            }
        }

        #region 赋值操作=================================
        private void ShowInfo()
        {
            SiteConfigBLL bll = new SiteConfigBLL();
            SiteConfigModel model = bll.loadConfig();

            webname.Text = model.WebName;
            weburl.Text = model.WebUrl;
            webcompany.Text = model.WebCompany;
            webaddress.Text = model.WebAddress;
            webtel.Text = model.WebTel;
            webmail.Text = model.WebMail;


            emailsmtp.Text = model.EmailSmtp;
            if (model.EmailSsl == 1)
            {
                emailssl.Checked = true;
            }
            else
            {
                emailssl.Checked = false;
            }


            emailport.Text = model.EmailPort.ToString();
            emailfrom.Text = model.EmailFrom;
            emailusername.Text = model.EmailNickName;
            if (!string.IsNullOrEmpty(model.EmailPassword))
            {
                emailpassword.Attributes["value"] = defaultPassword;
            }
            emailnickname.Text = model.EmailNickName;

            filepath.Text = model.FilePath;
            filesave.SelectedValue = model.FileSave.ToString();
            fileextension.Text = model.FileExtension;
            videoextension.Text = model.VideoExtension;
            attachsize.Text = model.AttachSize.ToString();
            videosize.Text = model.VideoSize.ToString();
            imgsize.Text = model.ImgSize.ToString();
            imgmaxheight.Text = model.ImgMaxHeight.ToString();
            imgmaxwidth.Text = model.ImgMaxWidth.ToString();
            thumbnailheight.Text = model.ThumbNailHeight.ToString();
            thumbnailwidth.Text = model.ThumbNailWidth.ToString();

        }
        #endregion


        /// <summary>
        /// 保存配置信息
        /// </summary>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("SysConfig", WeFrameEnums.ActionEnum.Edit.ToString()); //检查权限
            WeFrame.Core.BLL.SiteConfigBLL bll = new WeFrame.Core.BLL.SiteConfigBLL();
            WeFrame.Core.Model.SiteConfigModel model = bll.loadConfig();
            try
            {
                model.WebName = webname.Text;
                model.WebUrl = weburl.Text;
                model.WebCompany = webcompany.Text;
                model.WebAddress = webaddress.Text;
                model.WebTel = webtel.Text;
                model.WebMail = webmail.Text;





                model.EmailSmtp = emailsmtp.Text;
                if (emailssl.Checked == true)
                {
                    model.EmailSsl = 1;
                }
                else
                {
                    model.EmailSsl = 0;
                }
                model.EmailPort = Utils.StrToInt(emailport.Text.Trim(), 25);
                model.EmailFrom = emailfrom.Text;
                model.EmailUserName = emailusername.Text;
                //判断密码是否更改
                if (emailpassword.Text.Trim() != defaultPassword)
                {
                    model.EmailPassword = DESEncrypt.Encrypt(emailpassword.Text, "WeFrame");
                }
                model.EmailNickName = emailnickname.Text;

                model.FilePath = filepath.Text;
                model.FileSave = Utils.StrToInt(filesave.SelectedValue, 2);
                model.FileExtension = fileextension.Text;
                model.VideoExtension = videoextension.Text;
                model.AttachSize = Utils.StrToInt(attachsize.Text.Trim(), 0);
                model.VideoSize = Utils.StrToInt(videosize.Text.Trim(), 0);
                model.ImgSize = Utils.StrToInt(imgsize.Text.Trim(), 0);
                model.ImgMaxHeight = Utils.StrToInt(imgmaxheight.Text.Trim(), 0);
                model.ImgMaxWidth = Utils.StrToInt(imgmaxwidth.Text.Trim(), 0);
                model.ThumbNailHeight = Utils.StrToInt(thumbnailheight.Text.Trim(), 0);
                model.ThumbNailWidth = Utils.StrToInt(thumbnailwidth.Text.Trim(), 0);


                bll.saveConifg(model);
                JscriptMsg("修改系统配置成功！", "SiteConfig.aspx");
            }
            catch
            {
                JscriptMsg("文件写入失败，请检查文件夹权限！", "");
            }
        }

    }
}